/*
 * Copyright (C) 2013 Roy Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mogujie.coverflowsample.slice;

import com.dolphinwang.imagecoverflow.CoverFlowView;
import com.dolphinwang.imagecoverflow.LogUtil;
import com.mogujie.coverflowsample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

/**
 * Main Ability Slice
 */
public class MainAbilitySlice extends AbilitySlice {
    private CoverFlowView mCoverFlowView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        initData();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initView() {
        LogUtil.info(LogUtil.DEFAULT_TAG, "initView");
        Component component = findComponentById(ResourceTable.Id_coverflow);
        if (component instanceof CoverFlowView) {
            mCoverFlowView = (CoverFlowView) component;
        }
    }

    private void initData() {
        if (mCoverFlowView == null) {
            return;
        }
        mCoverFlowView.setReflectionHeight(30);
        mCoverFlowView.setVisibleImage(5);
        final MyCoverFlowAdapter adapter = new MyCoverFlowAdapter(this);
        mCoverFlowView.setAdapter(adapter);
        mCoverFlowView.setStateListener(new CoverFlowView.StateListener() {
            @Override
            public void imageOnTop(CoverFlowView v, int p, float f, float t, float r, float b) {
                LogUtil.error(LogUtil.DEFAULT_TAG, p + " on top!");
            }

            @Override
            public void invalidationCompleted(CoverFlowView view) {
            }
        });

        mCoverFlowView.setImageLongClickListener(
                (view, position) -> LogUtil.error(LogUtil.DEFAULT_TAG, "image long clicked ==>" + position));

        mCoverFlowView.setImageClickListener(
                (coverFlowView, position) -> {
                    LogUtil.error(LogUtil.DEFAULT_TAG, position + " clicked!");
                    coverFlowView.setSelection(position);
                });

        findComponentById(ResourceTable.Id_change_bitmap_button)
                .setClickedListener(
                        new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {
                                mCoverFlowView.setSelection(0);
                            }
                        });
    }
}
